<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Netty-Websocket</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

</head>
<body>
<form onSubmit="return false;">
    <label>ID</label><input type="text" name="uid" id="uid"/> <br/>
    <label>TEXT</label><input type="text" name="message" value="这里输入消息"/> <br/>
    <br/> <input type="button" value="发送ws消息"
                 onClick="send(':'+this.form.message.value)"/>
    <hr color="black"/>
    <h3>服务端返回的应答消息</h3>
    <textarea id="responseText" style="width: 1024px;height: 300px;"></textarea>
</form>
</body>

<script type="text/javascript">

    var socket;
    console.log("-----");
    var uid = 111;

    var xid = 222;
    //this.uuid(6);
    $("#uid").val(uid);

    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        console.log("---WebSocket--");
        socket = new WebSocket("ws://127.0.0.1:9090/websocket/alarmData?type=global&uid="+uid+"&areaIds=87");
        // socket = new WebSocket("ws://127.0.0.1:8086/websocket?xid=" + xid + "&uid=" + uid + "&type=local");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.value += event.data + "\r\n";
        };
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "Netty-WebSocket服务器。。。。。。连接  \r\n";
        };
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "Netty-WebSocket服务器。。。。。。关闭 \r\n";
        };
    } else {
        alert("您的浏览器不支持WebSocket协议！");
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(uid + message);
        } else {
            alert("WebSocket 连接没有建立成功！");
        }
    }

    function uuid(len) {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < len; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        // s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
        // s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
        // s[8] = s[13] = s[18] = s[23] = "";
        var uuid = s.join("");
        return uuid;
    }
</script>
</html>
